package com.cloud.gatewaySpringSecurity.handler;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import javax.security.auth.Subject;
import java.util.Collection;

/**
 * @author zhozixiong
 * @date 2023/03/02
 *
 * 自定义存储器
 */
public class AccountCodeAuthenticationToken extends AbstractAuthenticationToken {

    /**
     * 账号
     */
    private String account;
    /**
     * 验证码
     */
    private String code;

    public AccountCodeAuthenticationToken(String account, String code) {
        super((Collection) null);
        this.account = account;
        this.code = code;
        this.setAuthenticated(false);
    }
    public AccountCodeAuthenticationToken(String account, String code, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.account = account;
        this.code = code;
        this.setAuthenticated(true);
    }

    public String getAccount() {
        return account;
    }

    public String getCode() {
        return code;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        Assert.isTrue(!isAuthenticated, "Cannot set this openId to trusted - use constructor which takes a GrantedAuthority list instead");
        super.setAuthenticated(false);
    }

    /**
     * 父类抽象方法
     **/
    @Override
    public boolean implies(Subject subject) {
        return false;
    }

    @Override
    public Object getCredentials() {
        return account;
    }

    @Override
    public Object getPrincipal() {
        return code;
    }
}
